package cn.doitedu;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.BytesWritable;
import org.roaringbitmap.longlong.Roaring64Bitmap;

import java.io.IOException;
import java.util.Date;

public class BitmapAddDateV2UDF extends UDF {

    private static final Date INITIAL = DateUtil.parse("2000-01-01");

    public byte[] evaluate(BytesWritable bitmapBytesWritable, String activeDate) throws IOException {

        Roaring64Bitmap bitmap;
        if (bitmapBytesWritable == null) {
            bitmap = Roaring64Bitmap.bitmapOf();


        } else {

            byte[] bytes = bitmapBytesWritable.getBytes();

            // 将bitmap反序列化
            // bitmap_add_dt(t1.bitmap, '2024-04-25')
            bitmap = BitmapUtils.de(bytes);


        }

        long between = DateUtil.between(INITIAL, DateUtil.parse(activeDate), DateUnit.DAY);

        bitmap.add(between);

        return BitmapUtils.ser(bitmap);
    }

}
